package com.hckj.medical.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hckj.medical.entity.UserInfo;
import com.hckj.medical.service.IUserInfoService;
import com.hckj.medical.utils.Code;
import com.hckj.medical.utils.R;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hckj
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/medical/user-info")
public class UserInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    /**
     * 接诊业务查询病人的基本信息
     */
    @GetMapping("/findByuid/{id}")
    public R findByuid(@PathVariable Integer id){
        Map maps = userInfoService.selectByid(id);
        if (maps.entrySet().size() > 0){
            return new R(Code.GET_OK,maps,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
    /**
     * 查询所有就诊卡
     */
    @PostMapping("/findAll")
    public R selectAll(@RequestBody Map map){
        Page<Map> all = userInfoService.findAll(map);
        if (all != null){
            return new R(Code.GET_OK,all,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
    /**
     * 根据id批量删除(逻辑删除)
     */
    @PostMapping("/deletes")
    public R deletes(@RequestBody List<Integer> del){
        userInfoService.deletesByid(del);
        return new R(Code.DELETE_OK,"删除成功");
    }

    /**
     * 根据id查询查询基本信息
     * @param id
     * @return
     */
    @GetMapping("/selectByUid/{id}")
    public R selectByUid(@PathVariable Integer id){
        UserInfo userInfo = userInfoService.selectUserInfoByid(id);
        if (userInfo != null){
            return new R(Code.GET_OK,userInfo,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
    /**
     * 根据身份证号修改基本信息
     */
    @PostMapping("/updateByIdentity")
    public R updateByIdentitya(@RequestBody UserInfo userInfo){
        userInfoService.updateByIdentity(userInfo);
        return new R(Code.UPDATE_OK,"修改成功");
    }

    /**
     * 根据姓名模糊查询所有就诊人
     * @return
     */
    @GetMapping("/selectAll")
    public R selectAll(){
        List<UserInfo> allByname = userInfoService.findAllByname();
        if (allByname.isEmpty()){
            return new R(Code.GET_ERRO,"查询失败");
        }else {
            return new R(Code.GET_OK,allByname,"查询成功");
        }
    }


    //添加就诊卡
    @PostMapping("/insertUserinfo")
    public R insertUserinfo(@RequestBody UserInfo userInfo){
        Integer integer = userInfoService.insertUserinfo(userInfo);
        if (integer>0){
            return new R(Code.SAVE_OK,userInfo.getId(),"添加成功");
        }else {
            return new R(Code.SAVE_ERRO,"添加失败");
        }

    }

    @GetMapping("/selectUserInfoByid/{id}")
    public R selectUserInfoByid(@PathVariable Integer id){
        UserInfo userInfo = userInfoService.selectUserInfoByid(id);
        List<UserInfo> userInfoByid = userInfoService.findUserInfoByid(id);
        HashMap<Object, Object> ss = new HashMap<>();
        ss.put("userinfo",userInfo);
        ss.put("usercard",userInfoByid);
        if (ss.entrySet().size()>0){
            return new R(Code.GET_OK,ss,"登录成功");
        }else {
            return new R(Code.GET_ERRO,"登录失败");
        }
    }

}
